package com.sdcqjy.property.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.sdcqjy.mylibrary.Logger;
import com.sdcqjy.mylibrary.OtherUtils;
import com.sdcqjy.mylibrary.widget.flyindicaor.FlycoPageIndicaor;
import com.sdcqjy.property.R;
import com.sdcqjy.property.base.BannerMode;
import com.sdcqjy.property.base.SimpleBaseActivity;
import com.sdcqjy.property.mode.ImageMode;
import com.sdcqjy.property.mode.ListMsgMode;
import com.sdcqjy.property.presenter.AboutPresenter;
import com.sdcqjy.property.presenter.contract.AboutContract;
import com.sdcqjy.property.widget.BannerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 磁磁帅 on 2018/4/18.
 * 功能：
 */
public class AboutActivity extends SimpleBaseActivity<AboutPresenter> implements AboutContract.View {
    @BindView(R.id.viewPage)
    ViewPager viewPage;
    @BindView(R.id.indicator)
    FlycoPageIndicaor indicator;
    @BindView(R.id.webView)
    WebView webView;
    private BannerView bannerView;

    public static void open(Context context) {
        Intent intent = new Intent(context, AboutActivity.class);
        context.startActivity(intent);
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
        ButterKnife.bind(this);
        presenter = new AboutPresenter(this);

        bannerView = new BannerView(getActivity(), viewPage)
                .setTime(30000).setOnClickImageListener(new BannerView.OnClickImageListener() {
                    public void onClickImage(BannerMode mode) {
                        Object tag = mode.tag;
                        if (tag != null && tag instanceof ImageMode) {
                            ImageMode imageMode = (ImageMode) tag;
                            presenter.getDetailsMode(imageMode.documentID);
                        }
                    }
                });

        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setDomStorageEnabled(true);
        settings.setAllowFileAccess(true); //设置可以访问文件
        settings.setLoadsImagesAutomatically(true); //支持自动加载图片
        settings.setDefaultTextEncodingName("utf-8");//设置编码格式
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        webView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Logger.out(url);
                if (url.equals("about:blank") || url.startsWith("mailto:")) {
                } else if (url.startsWith("tel:")) {//电话
                    OtherUtils.callTelOfUri(getActivity(), url);
                } else {
                    webView.loadUrl(url);
                }
                return true;
            }
        });
    }

    protected void asynRun() {
        presenter.getImageList();
        presenter.getHtml();

        setH();
    }

    public void onResume() {
        super.onResume();
        bannerView.onResume();
    }

    public void onPause() {
        super.onPause();
        bannerView.onPause();
    }

    protected void onDestroy() {
        super.onDestroy();
        bannerView.onDestroy();
    }

    private void setH() {
        int width = viewPage.getWidth();
        if (width > 0) {
            int i = width / 2;
            ViewGroup.LayoutParams layoutParams = viewPage.getLayoutParams();
            if (layoutParams != null) {
                layoutParams.height = i;
            }
        } else viewPage.postDelayed(this::setH, 20);
    }

    @OnClick(R.id.imgBack)
    public void onViewClicked() {
        onBackPressed();
    }

    @OnClick(R.id.textBott1)
    public void onTextBott1Clicked() {
        presenter.openDetailsOfListOne(15);
        //CategoryListActivity.open(getActivity(), 15);
    }

    @OnClick(R.id.textBott2)
    public void onTextBott2Clicked() {
        presenter.openDetailsOfListOne(16);
        //CategoryListActivity.open(getActivity(), 16);
    }

    @OnClick(R.id.textBott3)
    public void onTextBott3Clicked() {
        presenter.openDetailsOfListOne(17);
        //CategoryListActivity.open(getActivity(), 17);
    }

    public void getImageListRet(List<ImageMode> data) {
        List<BannerMode> list = new ArrayList<>();
        for (ImageMode mode : data) list.add(mode.getBannerMode());
        bannerView.addImageViews(list);
        indicator.setViewPager(viewPage, list.size());
    }

    public void getDetailsModeRet(ListMsgMode data) {
        DetailsActivity.open(getActivity(), data);
    }

    public void getHtmlRet(ListMsgMode data) {
        String htmlPath = data.htmlPath;
        if (TextUtils.isEmpty(htmlPath)) return;
        webView.loadUrl(htmlPath);
    }
}
